import  * as types  from '../actions/types';
import {call,put,takeEvery,take} from 'redux-saga/effects'
import * as actions from "../actions/userAction";
import * as api from '../../api'
import 'regenerator-runtime/runtime'; //加这句 不然报错


var t = Date.now();

//进行登陆操作
function* logIn(action){
    console.info("正在执行saga..... action是：",action); //{name: "songdewu", password: "songdewu", age: 15}
    try{

        let user = new FormData();
        user.append("name",action.user.name);
        user.append("password",action.user.password);
        let url = api.domain.ajaxDomain+'/userlog/login';
        console.log(url);
        const fetchData = function(){
            return fetch(url,{
                method: 'POST',
                body:user   //这里我参数只有一个data,大家可以还有更多的参数
            })
                .then((response )=>{
                    console.log("登录返回参数是：",response);
                    //dispatch(NavigationActions.back());
                    return response.json();
                })
                .then(res=>{
                    return res;
                    //console.log(res);
                });
        };

        let result = yield call(fetchData);
        console.log("result:",result);
        if(result.code == 200){
            yield put(actions.login({phone:"15181805492",pwd:"qqqqqqq"}));
        }else{
            yield put(actions.loginError(result.message));
        }
    }catch(err){
        //console.error("失败",err);
        console.log("catch error",err);
        yield put(actions.loginError("失败！网络异常"));
    }



}



const rootSagas = function* rootSaga(){
    yield takeEvery(types.LOGIN_START,logIn)
}

export default rootSagas

